setwd("[your home directory]/IRF_Figures/InputData/")
rm(list = ls())
library(tidyselect)
library(readr)
library(lpirfs)
library(gridExtra)
library(ggpubr)
library(cowplot)
library(colorspace)
library(foreign)
library(haven)
library(magrittr)
library(dplyr)


#-----------------------------Clean Data For Charts-------------------------####
mindate=1
maxdate=121 # 121= run through 2019, 92=run thru 2012q2 


dfa_gini=read_csv("dfa_gini_networth_low.csv")
dfa_networth_shares <- read_csv("dfa_shares_bus_cycle2_networth_2019q3.csv")


CE_shares=dfa_networth_shares['corp_equ_mfs']
RE_shares=dfa_networth_shares['real_estate']
NW_shares=dfa_networth_shares['networth']
ASSETS_shares=dfa_networth_shares['assets']
Group_shares=t(matrix(unlist(NW_shares), nrow=6))
Group_shares=Group_shares[mindate:maxdate,]
Group_shares2=data.frame(rowSums(Group_shares[,1:2]), Group_shares[,3], rowSums(Group_shares[,4:5]))

Group_shares3=data.frame(rowSums(Group_shares[,1:3]),   rowSums(Group_shares[,4:6]))

TOP01=data.frame(Group_shares[,1:1])
TOP1a=data.frame(rowSums(Group_shares[,1:2]))
TOP10a=data.frame(rowSums(Group_shares[,1:3]))
TOP1=data.frame(Group_shares[,2:2])
TOP10=data.frame(Group_shares[,3:3])
BOT90=data.frame(rowSums(Group_shares[,4:6]))
P7090=data.frame(Group_shares[,4:4])
P5070=data.frame(Group_shares[,5:5])
BOT50=data.frame(Group_shares[,6:6])

Group_shares4=data.frame(TOP01,TOP1,TOP10)
colnames(Group_shares4) <- c("TOP01","TOP1","TOP10")
Group_shares5=data.frame(TOP01,TOP1)
colnames(Group_shares5) <- c("TOP01","TOP1")

CE_Group_shares=t(matrix(unlist(CE_shares), nrow=6))
CE_Group_shares=CE_Group_shares[mindate:maxdate,]
CE_Group_shares2=data.frame(rowSums(CE_Group_shares[,1:2]), CE_Group_shares[,3], rowSums(CE_Group_shares[,4:5]))


RE_Group_shares=t(matrix(unlist(RE_shares), nrow=6))
RE_Group_shares=RE_Group_shares[mindate:maxdate,]
RE_Group_shares2=data.frame(rowSums(RE_Group_shares[,1:2]), RE_Group_shares[,3], rowSums(RE_Group_shares[,4:5]))


ASSETS_Group_shares=t(matrix(unlist(ASSETS_shares), nrow=6))
ASSETS_Group_shares=ASSETS_Group_shares[mindate:maxdate,]
ASSETS_Group_shares2=data.frame(rowSums(ASSETS_Group_shares[,1:2]), ASSETS_Group_shares[,3], rowSums(ASSETS_Group_shares[,4:6]))



plotnames=c('toppoint1','top1', 'next9','prctile60to90','prctile30to60','prctile0to30')
plotnames2=c('top1', 'next9','prctile10to100')
plotnames3=c('top10','prctile10to100')

DFAGINI=dfa_gini['gini']
GINI=DFAGINI[mindate:maxdate,]

FFrate <- read.csv("FEDFUNDS.csv")
FFrate=FFrate[mindate:maxdate,]
FFR=FFrate['FEDFUNDS']
FFR2=c(9.73,FFR[,1])
FFR_diff=diff(FFR2,1)
FFR_diff=c(FFR_diff)


housing <- read.csv("CSUSHPISA_June2023.csv")
#annualized ror
house=housing['annualized']
HOUSE=as.data.frame(house[mindate:maxdate,])

# upload_shocks_june2023.csv -- includes the Gertler and Karadi 2015 shocks
MPShocks_all<- read.csv("upload_shocks_june2023.csv")
MPS_RR_shocks=MPShocks_all['RR_Original']
MPS_RR_shocks_ext=MPShocks_all['RR_Extended']
MPS_NS_shocks1=MPShocks_all['NS_FFR']
MPS_NS_shocks2=MPShocks_all['NS_News']
MPS_BRW_shocks=MPShocks_all['BRW']


# Romer and Romer
MPS_RR=as.data.frame(MPS_RR_shocks[mindate:maxdate,])
MPS_RR_ext=as.data.frame(MPS_RR_shocks_ext[mindate:maxdate,])
# Nakamura Steinsson
MPS_NS1=as.data.frame(MPS_NS_shocks1[mindate:maxdate,])
MPS_NS2=as.data.frame(MPS_NS_shocks2[mindate:maxdate,])
MPS_BRW=as.data.frame(MPS_BRW_shocks[mindate:maxdate,])

# New Gertler shocks (June 2023)
MPS_GK_shock1=MPShocks_all['GK_MP1']
MPS_GK_shock2=MPShocks_all['GK_FF4']

MPS_GK1=as.data.frame(MPS_GK_shock1[mindate:maxdate,])
MPS_GK2=as.data.frame(MPS_GK_shock2[mindate:maxdate,])


Inflation3<- read.csv("CPI.csv")
Inflation3=Inflation3[mindate:maxdate,]
CPI=Inflation3['CPIAUCSL_PCH']

INF=CPI

gdp<- read.csv("GDPGAP.csv")
gdp=gdp[mindate:maxdate,]
GDP=gdp['GDPC1_GDPPOT']

unemployment<- read.csv("UNRATE.csv")
unemployment=unemployment[mindate:maxdate,]
UNEMP=unemployment['UNRATE']


ugap<- read.csv("ugap.csv")
ugap=ugap[mindate:maxdate,]
UGAP=ugap['NROU_UNRATE']


sp500 <- read.csv("upload_sp500.csv")
sp500 =sp500['Return_ann']

sp=sp500[(mindate:maxdate),]
SP500=sp

STOCK= sp


plotselect=seq(6,36,6)
plotselect2=seq(4,16,4)
plotselect3=seq(3,9,3)


### HOUSE STOCK INF UNEMP GDP MP ####
shock="ALL7x7"

IRF_data_gini6=as.data.frame(cbind(GINI, HOUSE,STOCK,INF,UNEMP,GDP, -FFR))
results_lin_gini6= lp_lin(IRF_data_gini6, 
                          lags_endog_lin = 8,    # Number of lags for endogenous data
                          trend          = 1,    # 0 = no trend, 1 = trend, 2 = trend & trend^2    
                          shock_type     = 0,    # 0 = standard deviation shock, 1 = unit shock
                          confint        = 1.645,
                          hor=16
)
linear_plots_gini_all7 <- plot_lin(results_lin_gini6)


#--------------------------------Figure 12 Panel A--------------------------####
figure_12a <- function()
{
  #linear_plots_gini_all7[[2]][["labels"]][["title"]]="All / GINI through 2019"
  #linear_plots_gini_all7[[2]][["labels"]][["y"]]="All Effect on Wealth Gini Coefficient"
  linear_plots_gini_all7[[2]][["labels"]][["x"]]="Quarters"
  linear_plots_gini_all7[[2]][["theme"]][["axis.title.y"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[2]][["theme"]][["axis.title.x"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[2]][["theme"]][["axis.text"]][["colour"]]='black'
  linear_plots_gini_all7[[2]][["theme"]][["axis.text"]][["size"]]=10
  linear_plots_gini_all7[2]
}
figure_12a()

#--------------------------------Figure 12 Panel B--------------------------####
figure_12b <- function()
{
  #linear_plots_gini_all7[[3]][["labels"]][["title"]]="All / GINI through 2019"
  #linear_plots_gini_all7[[3]][["labels"]][["y"]]="All Effect on Wealth Gini Coefficient"
  linear_plots_gini_all7[[3]][["labels"]][["x"]]="Quarters"
  linear_plots_gini_all7[[3]][["theme"]][["axis.title.y"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[3]][["theme"]][["axis.title.x"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[3]][["theme"]][["axis.text"]][["colour"]]='black'
  linear_plots_gini_all7[[3]][["theme"]][["axis.text"]][["size"]]=10
  linear_plots_gini_all7[3]
}
figure_12b()

#--------------------------------Figure 12 Panel C--------------------------####
figure_12c <- function()
{
  #linear_plots_gini_all7[[4]][["labels"]][["title"]]="All / GINI through 2019"
  #linear_plots_gini_all7[[4]][["labels"]][["y"]]="All Effect on Wealth Gini Coefficient"
  linear_plots_gini_all7[[4]][["labels"]][["x"]]="Quarters"
  linear_plots_gini_all7[[4]][["theme"]][["axis.title.y"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[4]][["theme"]][["axis.title.x"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[4]][["theme"]][["axis.text"]][["colour"]]='black'
  linear_plots_gini_all7[[4]][["theme"]][["axis.text"]][["size"]]=10
  linear_plots_gini_all7[4]
}
figure_12c()

#--------------------------------Figure 12 Panel D--------------------------####
figure_12d <- function()
{
  #linear_plots_gini_all7[[5]][["labels"]][["title"]]="All / GINI through 2019"
  #linear_plots_gini_all7[[5]][["labels"]][["y"]]="All Effect on Wealth Gini Coefficient"
  linear_plots_gini_all7[[5]][["labels"]][["x"]]="Quarters"
  linear_plots_gini_all7[[5]][["theme"]][["axis.title.y"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[5]][["theme"]][["axis.title.x"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[5]][["theme"]][["axis.text"]][["colour"]]='black'
  linear_plots_gini_all7[[5]][["theme"]][["axis.text"]][["size"]]=10
  linear_plots_gini_all7[5]
}
figure_12d()

#--------------------------------Figure 12 Panel E--------------------------####
figure_12e <- function()
{
  #linear_plots_gini_all7[[6]][["labels"]][["title"]]="All / GINI through 2019"
  #linear_plots_gini_all7[[6]][["labels"]][["y"]]="All Effect on Wealth Gini Coefficient"
  linear_plots_gini_all7[[6]][["labels"]][["x"]]="Quarters"
  linear_plots_gini_all7[[6]][["theme"]][["axis.title.y"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[6]][["theme"]][["axis.title.x"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[6]][["theme"]][["axis.text"]][["colour"]]='black'
  linear_plots_gini_all7[[6]][["theme"]][["axis.text"]][["size"]]=10
  linear_plots_gini_all7[6]
}
figure_12e()

#--------------------------------Figure 12 Panel F--------------------------####
figure_12f <- function()
{
  #linear_plots_gini_all7[[7]][["labels"]][["title"]]="All / GINI through 2019"
  #linear_plots_gini_all7[[7]][["labels"]][["y"]]="All Effect on Wealth Gini Coefficient"
  linear_plots_gini_all7[[7]][["labels"]][["x"]]="Quarters"
  linear_plots_gini_all7[[7]][["theme"]][["axis.title.y"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[7]][["theme"]][["axis.title.x"]][["size"]]=c(10,10)
  linear_plots_gini_all7[[7]][["theme"]][["axis.text"]][["colour"]]='black'
  linear_plots_gini_all7[[7]][["theme"]][["axis.text"]][["size"]]=10
  linear_plots_gini_all7[7]
}
figure_12f()

#----------------------------------Export Series----------------------------####
setwd("[your home directory]/IRF_Figures/OutputData/")
export_figure12_series <- function()
{
  # Create Figure 12a Series 
  figure12aseries_high <- as.data.frame(t(results_lin_gini6$irf_lin_up[, , 2]))
  figure12aseries_high <- as.data.frame(figure12aseries_high$V1)
  
  figure12aseries_mean <- as.data.frame(t(results_lin_gini6$irf_lin_mean[, , 2]))
  figure12aseries_mean <- as.data.frame(figure12aseries_mean$V1)
  
  figure12aseries_low <- as.data.frame(t(results_lin_gini6$irf_lin_low[, , 2]))
  figure12aseries_low <- as.data.frame(figure12aseries_low$V1)
  
  # Create Figure 12b Series 
  figure12bseries_high <- as.data.frame(t(results_lin_gini6$irf_lin_up[, , 3]))
  figure12bseries_high <- as.data.frame(figure12bseries_high$V1)
  
  figure12bseries_mean <- as.data.frame(t(results_lin_gini6$irf_lin_mean[, , 3]))
  figure12bseries_mean <- as.data.frame(figure12bseries_mean$V1)
  
  figure12bseries_low <- as.data.frame(t(results_lin_gini6$irf_lin_low[, , 3]))
  figure12bseries_low <- as.data.frame(figure12bseries_low$V1)
  
  # Create Figure 12c Series 
  figure12cseries_high <- as.data.frame(t(results_lin_gini6$irf_lin_up[, , 4]))
  figure12cseries_high <- as.data.frame(figure12cseries_high$V1)
  
  figure12cseries_mean <- as.data.frame(t(results_lin_gini6$irf_lin_mean[, , 4]))
  figure12cseries_mean <- as.data.frame(figure12cseries_mean$V1)
  
  figure12cseries_low <- as.data.frame(t(results_lin_gini6$irf_lin_low[, , 4]))
  figure12cseries_low <- as.data.frame(figure12cseries_low$V1)
  
  # Create Figure 12d Series 
  figure12dseries_high <- as.data.frame(t(results_lin_gini6$irf_lin_up[, , 5]))
  figure12dseries_high <- as.data.frame(figure12dseries_high$V1)
  
  figure12dseries_mean <- as.data.frame(t(results_lin_gini6$irf_lin_mean[, , 5]))
  figure12dseries_mean <- as.data.frame(figure12dseries_mean$V1)
  
  figure12dseries_low <- as.data.frame(t(results_lin_gini6$irf_lin_low[, , 5]))
  figure12dseries_low <- as.data.frame(figure12dseries_low$V1)
  
  # Create Figure 12e Series 
  figure12eseries_high <- as.data.frame(t(results_lin_gini6$irf_lin_up[, , 6]))
  figure12eseries_high <- as.data.frame(figure12eseries_high$V1)
  
  figure12eseries_mean <- as.data.frame(t(results_lin_gini6$irf_lin_mean[, , 6]))
  figure12eseries_mean <- as.data.frame(figure12eseries_mean$V1)
  
  figure12eseries_low <- as.data.frame(t(results_lin_gini6$irf_lin_low[, , 6]))
  figure12eseries_low <- as.data.frame(figure12eseries_low$V1)
  
  # Create Figure 12f Series 
  figure12fseries_high <- as.data.frame(t(results_lin_gini6$irf_lin_up[, , 7]))
  figure12fseries_high <- as.data.frame(figure12fseries_high$V1)
  
  figure12fseries_mean <- as.data.frame(t(results_lin_gini6$irf_lin_mean[, , 7]))
  figure12fseries_mean <- as.data.frame(figure12fseries_mean$V1)
  
  figure12fseries_low <- as.data.frame(t(results_lin_gini6$irf_lin_low[, , 7]))
  figure12fseries_low <- as.data.frame(figure12fseries_low$V1)
  
  # Merge all series together
  figure12series <- list(figure12aseries_high, figure12aseries_mean, figure12aseries_low, figure12bseries_high, figure12bseries_mean, figure12bseries_low, figure12cseries_high, figure12cseries_mean, figure12cseries_low, figure12dseries_high, figure12dseries_mean, figure12dseries_low, figure12eseries_high, figure12eseries_mean, figure12eseries_low, figure12fseries_high, figure12fseries_mean, figure12fseries_low)
  figure12series <- as.data.frame(figure12series)
  figure12series <- (mutate(figure12series, quarter=row_number()))
  quarter <- as.data.frame(figure12series$quarter)
  figure12series <- list(quarter, figure12aseries_high, figure12aseries_mean, figure12aseries_low, figure12bseries_high, figure12bseries_mean, figure12bseries_low, figure12cseries_high, figure12cseries_mean, figure12cseries_low, figure12dseries_high, figure12dseries_mean, figure12dseries_low, figure12eseries_high, figure12eseries_mean, figure12eseries_low, figure12fseries_high, figure12fseries_mean, figure12fseries_low)
  figure12series <- as.data.frame(figure12series)
  figure12series <- rename(figure12series, quarters = `figure12series.quarter`, figure12ahigh = `figure12aseries_high.V1`, figure12bhigh = `figure12bseries_high.V1`, figure12chigh = `figure12cseries_high.V1`, figure12dhigh = `figure12dseries_high.V1`, figure12ehigh = `figure12eseries_high.V1`, figure12fhigh = `figure12fseries_high.V1`, figure12amean = `figure12aseries_mean.V1`, figure12bmean = `figure12bseries_mean.V1`, figure12cmean = `figure12cseries_mean.V1`, figure12dmean = `figure12dseries_mean.V1`, figure12emean = `figure12eseries_mean.V1`, figure12fmean = `figure12fseries_mean.V1`, figure12alow = `figure12aseries_low.V1`, figure12blow = `figure12bseries_low.V1`, figure12clow = `figure12cseries_low.V1`, figure12dlow = `figure12dseries_low.V1`, figure12elow = `figure12eseries_low.V1`, figure12flow = `figure12fseries_low.V1`)
  
  # Export the series 
  write.csv(figure12series, "figure12series.csv")
}
export_figure12_series()

#-------------------------------Figure 12 Chart Code------------------------####

figure12 <- read_csv("[your home directory]/IRF_Figures/OutputData/figure12series.csv") 
figure12data <- figure12 %>% filter(quarters<17)
theme_if_policystyle <- function() {
  small_margins <- ggplot2::theme(plot.margin = unit(c(2, 2, 2, 3.25), "mm"))
  
  theme_if_policystyle <- ggplot2::theme_classic() +
    theme(
      #text = element_text(family = "Arial"), commented out by m1aem03 due to user reported issues 10/17/2023
      axis.ticks.length = unit(-0.2, "cm"),
      # Remove panel border
      panel.border = ggplot2::element_blank(),
      plot.background = ggplot2::element_rect(fill = "transparent", colour = NA),
      plot.margin = ggplot2::unit(c(0.04, 0.175, 0, 0.175), "in"),
      
      axis.text = ggplot2::element_text(size = 6),
      axis.text.x = ggplot2::element_text(margin = ggplot2::margin(t = 3), color = "black", size = 6), # 10
      axis.text.y = ggplot2::element_text(margin = ggplot2::margin(r = 3), color = "black", size = 6), # 9.5
      axis.text.x.bottom = ggplot2::element_text(size = 6),
      axis.text.y.left = ggplot2::element_text(margin = ggplot2::margin(r = 3), size = 6, hjust = 1), # update 1.3.1 to handle y margin spacing issues
      axis.text.y.right = ggplot2::element_text(margin = ggplot2::margin(l = 3), size = 6, hjust = 1), # 9.5
      axis.title = ggplot2::element_text(size = 6, face = "plain"),
      
      plot.title = ggplot2::element_text(hjust = .5, size = 8, face = "bold"),
      plot.caption = ggplot2::element_text(size = 6, hjust = 0),
      plot.subtitle = ggplot2::element_text(size = 6, hjust = 1, margin = margin(0, 0, 0, 0, "pt")),
      
      axis.ticks = ggplot2::element_line(linewidth = 0.3, color = "black"),
      axis.line = ggplot2::element_line(linewidth = 0.3, color = "black"),
      axis.ticks.x = ggplot2::element_line(linewidth = 0.3, color = "black"),
      axis.line.x = ggplot2::element_line(linewidth = 0.3, color = "black"),
      axis.line.y = ggplot2::element_line(linewidth = 0.3, color = "black"),
      axis.ticks.y = ggplot2::element_line(linewidth = 0.3, color = "black"),
      
      legend.text = ggplot2::element_text(size = 8),
      legend.key.size = ggplot2::unit(0.4, 'cm'),
      legend.spacing.x = unit(0.05, 'cm'),
      legend.spacing.y = unit(-0.15, 'cm'),
      legend.key = ggplot2::element_blank(), # removes the legend title
      legend.title = ggplot2::element_blank(),
      legend.background = ggplot2::element_blank()
    )
  
  return(theme_if_policystyle)
}
light_color_set <- c("#afc98d", "#729cc3", "#cec68f", "darkgrey", "orange") 

runcharts <- function(){
#------------------------------Figure12a------------------------------------####
figure12a <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12amean, ymin = figure12ahigh, ymax = figure12alow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12amean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(A) Gini to House Prices", subtitle = "", caption = paste(" ", "", sep = "\n")) +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12a
#----------------------------Figure12a_ind----------------------------------####
figure12a_ind <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12amean, ymin = figure12ahigh, ymax = figure12alow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12amean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(A) Gini to House Prices", subtitle = "") +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12a_ind
#------------------------------Figure12b------------------------------------####
figure12b <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12bmean, ymin = figure12bhigh, ymax = figure12blow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12bmean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(B) Gini to Stock Prices", subtitle = "", caption = paste(" ", "", sep = "\n")) +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12b
#----------------------------Figure12b_ind----------------------------------####
figure12b_ind <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12bmean, ymin = figure12bhigh, ymax = figure12blow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12bmean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(B) Gini to Stock Prices") +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12b_ind
#------------------------------Figure12c------------------------------------####
figure12c <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12cmean, ymin = figure12chigh, ymax = figure12clow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12cmean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(C) Gini to Inflation", subtitle = "", caption = paste(" ", "", sep = "\n")) +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12c
#----------------------------Figure12c_ind----------------------------------####
figure12c_ind <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12cmean, ymin = figure12chigh, ymax = figure12clow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12cmean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(C) Gini to Inflation") +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12c_ind
#------------------------------Figure12d------------------------------------####
figure12d <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12dmean, ymin = figure12dhigh, ymax = figure12dlow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12dmean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(D) Gini to Unemployment Rate", subtitle = "") +
  scale_y_continuous(position = "right", breaks = seq(-.002, .0025, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12d
#------------------------------Figure12e------------------------------------####
figure12e <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12emean, ymin = figure12ehigh, ymax = figure12elow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12emean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(E) Gini to GDP Gap", subtitle = "") +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.25, 0.85))

figure12e
#------------------------------Figure12f------------------------------------####
figure12f <- ggplot(data = figure12data, aes(x = quarters)) +
  geom_ribbon(aes(y = figure12fmean, ymin = figure12fhigh, ymax = figure12flow, fill = "90% confidence band"), colour = "darkgrey", alpha = 0.4, size = .5, outline.type = "both") + # this creates the grey ribbon around the line
  geom_line(aes(y = figure12fmean, color = "Impulse response"), size = .8) +
  geom_hline(yintercept = 0, linetype = "dashed", size = .3) +
  scale_color_manual(name = "", values = c("Impulse response" = "black")) +
  scale_fill_manual(name = "", values = c("90% confidence band" = light_color_set[4])) +
  ylab(NULL) +
  xlab("Quarters") +
  labs(title = "(F) Gini to FFR", subtitle = "") +
  scale_y_continuous(position = "right", breaks = seq(-.002, .002, .0005), sec.axis = dup_axis(labels = NULL), expand = c(0, 0), limits = c(-.0017, .0015)) + # Sets y axis limits and ends the y-axis with a tick mark (caps the Y-axis) 
  scale_x_continuous(expand = c(.035, .035), breaks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), labels = c("","2","","4","","6","","8","","10","","12","","14","","16"))  + # Sets x axis limits
  theme_if_policystyle() + # --Theme from /if/appl/R/Functions/releases/1.4.0/theme_if_policystyle.r
  theme(legend.position = c(.3, 0.85))

figure12f

# Create Figures ####
fig12 <- ggarrange(figure12a, figure12b, figure12c, figure12d, figure12e, figure12f, ncol = 3, nrow = 2, legend = "bottom", common.legend = TRUE)
fig12a <- ggarrange(figure12a_ind, ncol = 1, nrow = 1, legend = "bottom", common.legend = TRUE)
fig12b <- ggarrange(figure12b_ind, ncol = 1, nrow = 1, legend = "bottom", common.legend = TRUE)
fig12c <- ggarrange(figure12c_ind, ncol = 1, nrow = 1, legend = "bottom", common.legend = TRUE)
fig12d <- ggarrange(figure12d, ncol = 1, nrow = 1, legend = "bottom", common.legend = TRUE)
fig12e <- ggarrange(figure12e, ncol = 1, nrow = 1, legend = "bottom", common.legend = TRUE)
fig12f <- ggarrange(figure12f, ncol = 1, nrow = 1, legend = "bottom", common.legend = TRUE)

# Save Figures ####
setwd("[your home directory]/IRF_Figures/Figure12/")
ggsave(fig12, file = "figure12.eps", height = 6, width = 9, units = "in", device = cairo_ps)
ggsave(fig12a, file = "figure12a.eps", height = 3, width = 3, units = "in", device = cairo_ps)
ggsave(fig12b, file = "figure12b.eps", height = 3, width = 3, units = "in", device = cairo_ps)
ggsave(fig12c, file = "figure12c.eps", height = 3, width = 3, units = "in", device = cairo_ps)
ggsave(fig12d, file = "figure12d.eps", height = 3, width = 3, units = "in", device = cairo_ps)
ggsave(fig12e, file = "figure12e.eps", height = 3, width = 3, units = "in", device = cairo_ps)
ggsave(fig12f, file = "figure12f.eps", height = 3, width = 3, units = "in", device = cairo_ps)
}
runcharts()